﻿using Abp.Auditing;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.Extensions;
using Abp.Runtime.Session;
using Microsoft.AspNetCore.Mvc;
using Mt.Site.Application.ContentManagement.FrontEnd;
using Mt.Site.Application.ContentManagement.FrontEnd.Dto;
using Mt.Site.Application.Taxonomys;
using Mt.Site.Core.Base.Domain.Entities.ShortId;
using Mt.Site.Core.ContentManagement.Entities;
using Mt.Site.Core.ContentManagement.Layout;
using Mt.Site.Core.ContentManagement.Preview;
using Mt.Site.Core.Navigations;
using Mt.Site.Core.Url;
using Mt.Site.Core.Web;
using Mt.Site.Web.Controllers;
using System;
using System.Threading.Tasks;

namespace Mt.Site.Web.Areas.B.Controllers
{
    public class PreviewController : MtSiteControllerBase
    {
        private readonly IContentFrontEndAppService _content;
        private readonly IMenuStore _menuStore;
        private readonly ITenancyNameCache _tenancyNameCache;
        private readonly ITaxonomyFrontAppService _taxonomy;
        public IShortIdManager ShortIdManager { get; set; }
        private readonly IContent2FrontEndAppService _content2;
        private readonly IPreviewDataManager _previewData;
        private readonly ILayoutEditorManager _layoutEditorManager;
        private readonly IPageUrlHelper _urlHelper;
        private readonly IRepository<ContentBase, Guid> _contentRepository;

        public PreviewController(
            IMenuStore menuStore,
                IContentFrontEndAppService content,
                IContent2FrontEndAppService content2,
                ITaxonomyFrontAppService taxonomy,
                IPageUrlHelper urlHelper,
                ITenancyNameCache tenancyNameCache,
                IPreviewDataManager previewData,
                ILayoutEditorManager layoutEditorManager,
                IRepository<ContentBase, Guid> contentRepository)
        {
            _menuStore = menuStore;
            _content = content;
            _content2 = content2;
            _urlHelper = urlHelper;
            _taxonomy = taxonomy;
            _tenancyNameCache = tenancyNameCache;
            _previewData = previewData;
            _layoutEditorManager = layoutEditorManager;
            _contentRepository = contentRepository;
        }

        [DisableAuditing]
        public async Task<ActionResult> Index(string token)
        {
            var cacheData = await _previewData.GetPreviewCacheDataAsync(token);
            var CacheItem = cacheData.CacheItem;
            var Url = "/Preview/NewsPreview?k=" + CacheItem.MenuId + "&token=" + token;
            if (CacheItem.Key == "case")
            {
                Url = "/Preview/CasePreview?k=" + CacheItem.MenuId + "&token=" + token;
            }
            else if (CacheItem.Key == "活动集锦")
            {
                Url = "/Preview/activityPreview?k=" + CacheItem.MenuId + "&token=" + token;
            }

            return Redirect(Url);
        }


        [DisableAuditing]
        public async Task<ActionResult> NewsPreview(long k, string token)
        {
            var item = await _menuStore.GetMenuIdTraceCacheItemAsync(k);

            var cacheData = await _previewData.GetPreviewCacheDataAsync(token);
            var CacheItem = cacheData.CacheItem;

            var tenancyNameCache = _tenancyNameCache.As<TenancyNameCache>();
            tenancyNameCache.TenantId = CacheItem.TenantId.Value;
            tenancyNameCache.CultureName = CacheItem.CultureName;
            tenancyNameCache.IsNeedTenancyName = false;
            tenancyNameCache.IsNeedCultureName = false;

            //var viewModel = new NewsDetailViewModel();

            var title = "";

            using (AbpSession.Use(CacheItem.TenantId, CacheItem.UserId, CacheItem.CultureName))
            using (UnitOfWorkManager.Current.SetTenantId(CacheItem.TenantId))
            using (UnitOfWorkManager.Current.SetCultureName(CacheItem.CultureName))
            {
                //viewModel.Detail = await _content2._GetContentPreviewAsync(new PreviewContent2Input { Guid = CacheItem.Id, MenuId = k, PicKeys = "slt" });

                //var kind = await ShortIdManager.GetShortIdAsync(k);
                //var id = await ShortIdManager.GetShortIdAsync(viewModel.Detail.AuxiliaryId);

                //ViewBag.kind = k;
                //ViewBag.DisplayName = item.DisplayName;
                //ViewBag.id = id;
                //ViewBag.RelatedId = item.Id();
                //ViewBag.IsDetail = id;
                //ViewBag.DefaultTitle = viewModel.Detail.Title;

                title = (await _content._GetContentPreviewAsync(new _ContentInput { Guid = CacheItem.Id, PicKeys = "slt" }))?.Title;
            }

            //return View("~/Views/Press/NewsDetail.cshtml", viewModel);

            return Content("Preview Test:<br>" + title + "<br> 过期时间："+ cacheData.CacheExpireTime.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }
}
